//
//  main.m
//  OC06_集合遍历_数组排序
//
//  Created by Scott on 16/3/2.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

#pragma mark - 知识点1. for 循环遍历.
void chapter1() {
    
#pragma mark 1.1 数组遍历.
#if 0
    NSArray *arr = @[@"zhangsan", @"Tom", @"Lisi"];
    
    for (int i = 0; i < arr.count; i++) {
        
        NSString *string = [arr objectAtIndex:i];
        
        NSLog(@"string:%@", string);
    }
    
    
    /**
     * 练习:
     *
     * 使用for循环完成:
     * 定义一个数组, 包含5个字符串对象, 倒序取出数组中所有元素, 并存储到另一可变数组中。
     */
    
    NSMutableArray *mArr = [NSMutableArray array];
    
    for (NSInteger i = arr.count - 1; i >= 0; i--) {
        
        [mArr addObject:[arr objectAtIndex:i]];
        
    }
    
    NSLog(@"mArr:%@", mArr);
    
#endif
#pragma mark 1.2 字典遍历.
#if 1
    /**
     * 练习:
     *
     * 使用for循环完成:
     * 定义一个字典, 用于存储一个人的name, sex, age三个数据, 完成对其三个数据的输出。
     */
    NSDictionary *dic = @{@"name":@"zhangsan",
                          @"sex" :@"male",
                          @"age" :@"20"};
    
    
    // 获取所有key, 然后对这个可以数组进行遍历.
    NSArray *arrKeys = dic.allKeys;
    
    for (NSInteger i = 0; i < arrKeys.count; i++) {

        NSString *key = [arrKeys objectAtIndex:i];
        
        NSString *value = [dic objectForKey:key];
        
        NSLog(@"value:%@", value);
    }
    
#endif
#pragma mark 1.3 集合遍历.
    
    NSSet *set = [NSSet setWithObjects:@"zhangsan", @"lisi", @"Tom", nil];
    
    NSArray *arr = set.allObjects;
    
    for (int i = 0; i < arr.count; i++) {
        
        NSString *str = [arr objectAtIndex:i];
        
        NSLog(@"str:%@", str);
    }
    
    

    
}


#pragma mark - 知识点2. NSEnumerator(枚举器) 遍历.
void chapter2() {
#pragma mark 2.1 数组正序枚举.
#if 0
    NSArray *arr = @[@"zhangsan", @"Tom", @"Lisi"];
    
    // 核心API, 详见API.
    NSEnumerator *enumerator = [arr objectEnumerator];
    
    id value;
    
    while (value = [enumerator nextObject]) {
        
        NSLog(@"value:%@", value);
        
    }
#endif
    
#pragma mark 2.2 数组反向枚举.
#if 0
    NSArray *arr = @[@"zhangsan", @"Tom", @"Lisi"];
    
    // 核心API.
    NSEnumerator *enumerator = [arr reverseObjectEnumerator];
    
    id value;
    while (value = [enumerator nextObject]) {
        
        NSLog(@"value:%@", value);
    }
#endif

#pragma mark 2.3 字典枚举.
#if 0
    
    NSDictionary *dic = @{@"name":@"zhangsan",
                          @"age" :@"20",
                          @"sex" :@"male"};
    
    // 核心API
    NSEnumerator *enumerator = [dic objectEnumerator];
    
    id value;
    while (value = [enumerator nextObject]) {
        
        NSLog(@"value:%@", value);
    }
    
#endif

#pragma mark 2.4 集合枚举.
    
    NSSet *set = [NSSet setWithObjects:@"zhangsan", @"lisi", @"Tom", nil];
    
    // 核心API
    NSEnumerator *enumertor = [set objectEnumerator];
    
    id value;
    while (value = [enumertor nextObject]) {
        
        NSLog(@"value:%@", value);
        
    }
    
    
    
}


#pragma mark - 知识点3. !!! for...in 遍历.
void chapter3() {
#pragma mark 3.1 数组遍历.
 
#if 0
    NSArray *arr = @[@"zhangsan", @"Tom", @"Lisi"];
    
    for (NSString *string in arr) {
        NSLog(@"string:%@", string);
    }
    
#endif
    
    
#if 0
    /**
     * 练习:
     *
     * 使用 for...in 完成:
     * 定义一个数组, 包含5个字符串对象, 取出数组中所有字符串, 并拼接生成一个新的字符串。
     */
    
    NSArray *arr = @[@"zhangsan", @"Tom", @"Lisi"];
    
    NSMutableString *mStr = [NSMutableString string];
    
    // 数组forin遍历时, 遍历的是里面的对象.
    for (NSString *string in arr) {
        
        [mStr appendString:string];
    }
    NSLog(@"mStr:%@", mStr);
    
#endif
    
#pragma mark 3.2 字典遍历.
#if 0
    NSDictionary *dic = @{@"name":@"zhangsan",
                          @"age" :@"20",
                          @"sex" :@"male"};
    
    // forin对字典遍历时, 遍历的是 key.
    for (NSString *key in dic) {
        
        NSString *value = [dic objectForKey:key];
        NSLog(@"value:%@", value);
    }
#endif
    
    /**
     * 练习:
     *
     * 使用 for...in... 完成:
     * 定义一个字典, 用于存储一个人的name, sex, age三个数据,  for...in...遍历, 输出此人的name到控制台
     */
#if 1
    
    NSDictionary *dic = @{@"name":@"zhangsan",
                          @"age" :@"20",
                          @"sex" :@"male"};
    
    for (NSString *key in dic) {
        
        if ([key isEqualToString:@"name"]) {
            
            NSString *value = [dic objectForKey:key];
            NSLog(@"value:%@", value);
        }
    }

#endif
    

#pragma mark 3.3 集合遍历.
    
    NSSet *set = [NSSet setWithObjects:@"zhangsan", @"Tom", @"lisi", nil];
    
    // forin对NSSet遍历时, 遍历里面的对象.
    for (NSString *str in set) {
        
        NSLog(@"str:%@", str);
    }
    
    
    
    
}


#pragma mark - 知识点4. 数组排序.
void chapter4() {
    
#pragma mark 4.1 NSSortDescriptor
#if 0
    NSArray *arr = @[@"zhangsan", @"Tom", @"Lisi"];
    
    NSSortDescriptor *des1 = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
    
    // 核心API
    NSArray *sortedArr = [arr sortedArrayUsingDescriptors:@[des1]];
    
    NSLog(@"sortedArr:%@", sortedArr);

#endif
    
#pragma mark 4.2 selector
    
    NSArray *arr = @[@"Zhangsan", @"Lisi", @"wangwu"];
    
    
    // 核心API. 注意: @selector(方法), 此方法一定是数组中对象的方法, 并且这个方法的返回值类型是NSComparisonResult, 同时带参数(类型是数组中的对象类型).
    NSArray *sortedArr = [arr sortedArrayUsingSelector:@selector(compare:)];
    
    NSLog(@"sortedArr:%@", sortedArr);
    
    
    /**
     * 练习:
     *
     * 生成一个包含5个Person对象的可变数组
     
     使用sortedArrayUsingSelector: 按姓名降序排序
     
     使用sortUsingSelector: 按年龄升序排序
     */
    Person *per1 = [Person personWithName:@"zhangsan" age:20];
    
    Person *per2 = [Person personWithName:@"Tom" age:18];
    
    Person *per3 = [Person personWithName:@"Lisi" age:19];
    
    NSArray *arrOfPerson = @[per1, per2, per3];
    
    // 名字降序
    NSArray *arrOfName = [arrOfPerson sortedArrayUsingSelector:@selector(DeSortedByName:)];
    
    for (Person *per in arrOfName) {
        NSLog(@"name:%@", per.name);
    }
    
    // 年龄升序
    NSArray *arrOfAge = [arrOfPerson sortedArrayUsingSelector:@selector(asSortedByAge:)];
    
    for (Person *person in arrOfAge) {
        NSLog(@"%@, %ld", person.name, person.age);
    }
    
    
    
}


#pragma mark - main 函数
int main(int argc, const char * argv[]) {
    
//    chapter1();
//    chapter2();
//    chapter3();
    chapter4();
    
    return 0;
}
